from pydantic import BaseModel, Field, validator
from datetime import datetime
from typing import List, Optional


class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=50)


class UserCreateInput(BaseModel):
    """用户创建输入模型 - 用于接收用户输入"""
    username: str = Field(..., min_length=3, max_length=50)
    password: str = Field(..., min_length=8, max_length=16)
    nickname: Optional[str] = Field(None, max_length=50, description="昵称")


class UserCreate(UserBase):
    """用户创建模型 - 用于内部处理，支持哈希后的密码"""
    password: str = Field(..., min_length=8)  # 移除 max_length 限制，因为哈希后的密码会更长


class UserUpdate(BaseModel):
    """用户更新模型"""
    nickname: Optional[str] = Field(None, max_length=50, description="昵称")
    email: Optional[str] = Field(None, max_length=255, description="邮箱")
    # 在实际项目中，可能还需要更新角色、部门等


class LoginForm(BaseModel):
    """用户登录的参数模型"""
    username: str = Field(description="账号", max_length=255, min_length=3)
    password: str = Field(description="密码", max_length=16, min_length=8)


class UserInfo(BaseModel):
    """用户信息的参数模型, 用于返回给用户"""
    id: int = Field(description="用户ID")
    username: str = Field(description="账号")
    nickname: Optional[str] = Field(None, description="昵称")
    email: Optional[str] = Field(None, description="邮箱")
    is_active: bool = Field(description="是否激活")
    created_at: datetime = Field(description="创建时间")

    class Config:
        from_attributes = True


class UserListResponse(BaseModel):
    """用户列表响应模型"""
    total: int = Field(description="总数")
    items: List[UserInfo] = Field(description="用户列表")


class Token(BaseModel):
    """Token模型"""
    access_token: str = Field(description="访问令牌")
    token_type: str = Field(description="令牌类型", default="bearer")


class TokenData(BaseModel):
    """Token数据模型"""
    username: str = Field(description="用户名")


class UserCreatedResponse(BaseModel):
    """用户创建成功的响应模型"""
    username: str = Field(description="用户名")
